<style lang="less">
    @import '../../styles/common.less';
</style>

<template>
    <div>
        <Col span="24">
            <Card>
                <p slot="title">
                    <Icon type="mouse"></Icon>
                    用户数据
                </p>
                <Row>
                    <Input v-model="userName" placeholder="请输入用户名..." style="width: 200px"/>
                    <Select v-model="statusModell" placeholder="--请选择状态--" style="width:200px">
                        <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                    </Select>
                    <span @click="handleSearchs()" style="margin: 0 10px;"><Button type="primary"
                                                                    icon="search">搜索</Button></span>
                    <span @click="handleRefreshs()" style="margin: 0 10px;"><Button type="primary"
                                                                    icon="refresh">刷新</Button></span>
                </Row>
                <Row>
                    <Button style="width: auto;text-align:right;margin:15px 15px 0 0;" icon="person-add" type="primary"
                            @click="showModal = true" long>{{ $t('btnAddText') }}
                    </Button>
                    <Modal v-model="showModal" title="新增用户" :mask-closable="false">
                        <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" v-model="showModal">
                            <FormItem label="Password" prop="passwd">
                                <Input type="password" v-model="formCustom.passwd"></Input>
                            </FormItem>
                            <FormItem label="Confirm" prop="passwdCheck">
                                <Input type="password" v-model="formCustom.passwdCheck"></Input>
                            </FormItem>
                            <FormItem label="Age" prop="age">
                                <Input type="text" v-model="formCustom.age" number></Input>
                            </FormItem>
                        </Form>
                        <div slot="footer">
                            <Button type="ghost" @click="handleReset()" style="margin-left: 8px">取消</Button>
                            <Button type="primary" @click="handleSubmit()" :loading="loading">新增</Button>
                        </div>
                    </Modal>
                </Row>
                <br/>
                <Row>
                    <can-edit-table
                            refs="userTable"
                            v-model="userData"
                            @on-cell-change="handlerCellChange"
                            @on-change="handlerChange"
                            @on-delete="handlerDelete"
                            :editIncell="true"
                            :columns-list="columns"
                    ></can-edit-table>
                </Row>
                <Row class="margin-top-10" style="text-align: right;padding-right:20px;">
                    <Page :total="total" :current="pageNum" :page-size="pageSize" @on-change="handleCurrentChange"
                          @on-page-size-change="handleSizeChange"
                          size="small" show-elevator show-sizer></Page>
                </Row>
            </Card>
        </Col>
    </div>
</template>

<script>
    import {mapActions} from 'vuex';
    import canEditTable from './components/canEditTable.vue';
    import * as table from './data/column';

    export default {
        name: 'user-table',
        components: {
            canEditTable
        },
        data() {
            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('Please enter your password'));
                } else {
                    if (this.formCustom.passwdCheck !== '') {
                        // 对第二个密码框单独验证
                        this.$refs.formCustom.validateField('passwdCheck');
                    }
                    callback();
                }
            };
            const validatePassCheck = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('Please enter your password again'));
                } else if (value !== this.formCustom.passwd) {
                    callback(new Error('The two input passwords do not match!'));
                } else {
                    callback();
                }
            };
            const validateAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('Age cannot be empty'));
                }
                // 模拟异步验证效果
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('Please enter a numeric value'));
                    } else {
                        if (value < 18) {
                            callback(new Error('Must be over 18 years of age'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                lang: 'zh-CN',
                valueText: 3,
                showModal: false,
                loading: false,
                userName: '',
                columns: table.userColumns,
                userData: [],
                pageNum: 1,
                pageSize: 10,
                total: 0,
                formCustom: {
                    passwd: '',
                    passwdCheck: '',
                    age: ''
                },
                ruleCustom: {
                    passwd: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    passwdCheck: [
                        {validator: validatePassCheck, trigger: 'blur'}
                    ],
                    age: [
                        {validator: validateAge, trigger: 'blur'}
                    ]
                },
                statusList: [
                    {
                        value: '0',
                        label: '初始'
                    },
                    {
                        value: '1',
                        label: '正常'
                    },
                    {
                        value: '2',
                        label: '冻结'
                    },
                    {
                        value: '9',
                        label: '删除'
                    }
                ],
                statusModell: ''
            };
        },
        methods: {
            ...mapActions(['getMemberUsers']),
            handleGetUser() {
                this.getMemberUsers({
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                    userName: this.userName,
                    status: this.statusModell
                }).then(res => {
                    if (res.data) {
                        this.total = parseInt(res.data.total);
                        this.pageNum = res.data.pageNum;
                        this.userData = res.data.list;
                    } else {
                        this.$Message.error(this.$t('queryDataFailed'));
                    }
                });
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleGetUser();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.handleGetUser();
            },
            handlerCellChange(val, index, key) {
                this.$Message.success('修改了第 ' + (index + 1) + ' 行列名为 ' + key + ' 的数据, 值为 ' + val[index][key]);
            },
            handlerChange(val, index) {
                this.$Message.success('修改了第' + (index + 1) + '行数据');
            },
            handlerDelete(val, index) {
                this.$Message.success('删除了第' + (index + 1) + '行数据');
            },
            handleSubmit() {
                this.loading = true;
                this.$refs['formCustom'].validate((valid) => {
                    if (valid) {
                        this.addUser({
                            userName: 'test123',
                            type: '00',
                            password: this.formCustom.passwd,
                            confirm: this.formCustom.passwdCheck
                        }).then(res => {
                            if (res && res.code === '0000') {
                                this.total = res.data.total;
                                this.pageNum = res.data.pageNum;
                                this.userData = res.data.list;
                                this.handleReset();
                                this.showModal = false;
                                this.$Message.success('新增用户成功!');
                            } else {
                                this.$Message.success('新增用户失败!');
                            }
                        });
                    } else {
                        this.loading = false;
                        this.$Message.error('参数不合法!');
                    }
                });
            },
            handleReset() {
                this.showModal = false;
                this.$refs['formCustom'].resetFields();
            },
            handleSearchs() {
                this.handleGetUser();
            },
            handleRefreshs() {
                this.handleGetUser();
            }
        },
        created() {
            this.handleGetUser();
        }
    };
</script>
